7385c8
@@ -143,8 +143,15 @@
private FetchTask optimize(ParseContext pctx, String alias, TableScanOperator so
   }
 
   private boolean checkThreshold(FetchData data, int limit, ParseContext pctx) throws Exception {
-    if (limit > 0 && data.hasOnlyPruningFilter()) {
-      return true;
+    if (limit > 0) {
+      if (data.hasOnlyPruningFilter()) {
+        /* partitioned table + query has only pruning filters */
+        return true;
+      } else if (data.isPartitioned() == false && data.isFiltered() == false) {
+        /* unpartitioned table + no filters */
+        return true;
+      }
+      /* fall through */
     }
     long threshold = HiveConf.getLongVar(pctx.getConf(),
         HiveConf.ConfVars.HIVEFETCHTASKCONVERSIONTHRESHOLD);
@@ -228,6 +235,10 @@
private FetchData checkOperators(FetchData fetch, TableScanOperator ts, boolean
       if (op.getChildOperators() == null || op.getChildOperators().size() != 1) {
         return null;
       }
+
+      if (op instanceof FilterOperator) {
+        fetch.setFiltered(true);
+      }
     }
 
     if (op instanceof FileSinkOperator) {
@@ -279,6 +290,11 @@
private boolean isConvertible(FetchData fetch, Operator<?> operator, Set<Operato
         || operator instanceof ScriptOperator) {
       return false;
     }
+
+    if (operator instanceof FilterOperator) {
+      fetch.setFiltered(true);
+    }
+
     if (!traversed.add(operator)) {
       return true;
     }
@@ -314,6 +330,7 @@
private boolean isConvertible(FetchData fetch, Operator<?> operator, Set<Operato
 
     // this is always non-null when conversion is completed
     private Operator<?> fileSink;
+    private boolean filtered;
 
     private FetchData(TableScanOperator scanOp, ReadEntity parent, Table table, SplitSample splitSample) {
       this.scanOp = scanOp;
@@ -337,10 +354,23 @@
private FetchData(TableScanOperator scanOp, ReadEntity parent, Table table, Prun
     /*
      * all filters were executed during partition pruning
      */
-    public boolean hasOnlyPruningFilter() {
+    public final boolean hasOnlyPruningFilter() {
       return this.onlyPruningFilter;
     }
 
+    public final boolean isPartitioned() {
+      return this.table.isPartitioned();
+    }
+
+    /* there are filter operators in the pipeline */
+    public final boolean isFiltered() {
+      return this.filtered;
+    }
+
+    public final void setFiltered(boolean filtered) {
+      this.filtered = filtered;
+    }
+
     private FetchWork convertToWork() throws HiveException {
       inputs.clear();
       if (!table.isPartitioned()) {
